const router = require('koa-router')();
router.prefix(appConfig.authPrefixUrl+'/desktop/phone_datas');
const PhoneDatasService = appRequire('service/desktop/PhoneDatasService.js');
const ResultVo = appRequire('vo/ResultVo.js');

router.get('/index',async(ctx)=>{
  ctx.body="phone_datas 你好";
});

router.post('/save',async(ctx)=>{
  try{
    let phoneDatas = ctx.request.body;
    console.log(phoneDatas);
    await PhoneDatasService.save(phoneDatas);
    ctx.body = ResultVo.success('success');
  }catch(err){
    //集中处理错误
    err.msg = "新增电话数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/deleteOne',async(ctx)=>{
  try{
    let phoneDatas = ctx.query;
    phoneDatas = await PhoneDatasService.deleteOne(phoneDatas.id);
    ctx.body = ResultVo.success('success',phoneDatas);
  }catch(err){
    //集中处理错误
    err.msg = "删除电话数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.post('/deleteMany',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await PhoneDatasService.deleteMany(param.ids);
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "批量删除电话数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findOne',async(ctx)=>{
  try{
    let phoneDatas = ctx.query;
    phoneDatas = await PhoneDatasService.findOne(phoneDatas.id);
    console.log(phoneDatas);
    ctx.body = ResultVo.success('success',phoneDatas);
  }catch(err){
    //集中处理错误
    err.msg = "查找电话数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});
router.get('/findAll',async(ctx)=>{
  try{
    let data = await PhoneDatasService.findAll();
    ctx.body = ResultVo.success('success',data);
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取全部电话数据数据失败!');
  }
});
router.get('/findByPage',async(ctx)=>{
  try{
    let page = ctx.query;
    console.log(page);
    let data = await PhoneDatasService.findByPage(page);
    ctx.body = data;
  }catch(error){
    ctx.body = ResultVo.error(-1,'获取分页电话数据数据失败!');
  }
});
router.post('/publishData',async(ctx)=>{
  try{
    let param = ctx.request.body;
    console.log(param);
    param = await PhoneDatasService.publishData();
    ctx.body = ResultVo.success('success',param);
  }catch(err){
    //集中处理错误
    err.msg = "发布电话数据失败!";
    ctx.app.emit('error', err, ctx);
  }
});

module.exports = router;